package com.chixing.day13_list;



import java.util.Iterator;
import java.util.LinkedList;

public class LinkedListDemo {

    public static void main(String[] args) {
        //LinkedList
        //（1）添加2个学生对象
        LinkedList<Student> link = new LinkedList<>();
        Student student1 = new Student("kirby",12);
        Student student2 = new Student("katy",23);
        link.add(student1);
        link.add(student2);
        System.out.println(link);
        // (2) 获得元素个数
        System.out.println("元素个数: " + link.size());
        // (3) 添加一个新学生在第一个位置上
        Student student3 = new Student("万叶",23);
        link.addFirst(student3);
        System.out.println(link);
        // (4) 获得名称为"katy" 的学生，对象
        System.out.println("------");
        Iterator<Student> iterator = link.iterator();
        while (iterator.hasNext()){
            Student student = iterator.next();
            if (student.getStuName().equals("katy"));
            System.out.println(student);

        }

        //  (5)迭代遍历集合

        System.out.println("------");
        Iterator<Student> iterator1 = link.iterator();
        while (iterator1.hasNext()){
            Student student = iterator1.next();
            System.out.println(student);
        }
        //   (6) 获得第2个学生
        System.out.println(" 获得第2个学生:" + link.get(2));
        //   (7) 删除第1个学生对象
        System.out.println(" 删除第1个学生对象:" + link.remove(1));
         //  (8)将最后一个学生的年龄改为22
        link.getLast().setStuAge(22);
        System.out.println(link.getLast());
    }
}
